var commonList = function () {
    return {
        init: function (listParam) {
			function getList(){
				//设置每页记录条数的select
				$('#'+listParam.listId+' select[name="pageNum"]').val(listParam.pageNum);
				
				//列表异步加载图标
				$('#'+listParam.listId).html('<div id="ajaxLoadingIco" style="text-align:center;padding:20px;"><img src="'+ABS_URL_Theme+'/Assets/img/ajax-loading.gif"/></div>');
				if(typeof listParam.table == "undefined"){
					listParam.table = '';		
				}
				
				$.get(
					listParam.jsonUrl,
					{
						where: escape(listParam.where),
						pageNum: listParam.pageNum,
						p: listParam.nowPage,
						orderName: listParam.orderName,
						orderDirection: listParam.orderDirection,
						table:listParam.table
						
					},
					function(data){
						if($.isEmptyObject(data.rows)){
							$('#'+listParam.listId).html('<div id="ajaxLoadingIco" style="text-align:center;padding:20px;">'+list_itf.no_record+'</div>');
							
							//页码记录显示0
							var str_record = '';
							str_record += '<select name="pageNum" class="m-wrap small">';
							str_record += '<option value="5">5</option>';
							str_record += '<option value="10">10</option>';
							str_record += '<option value="20">20</option>';
							str_record += '<option value="50">50</option>';
							str_record += '<option value="100">100</option>';
						  //str_record += '<option value="-1">All</option>';
							str_record += '</select> '+list_itf.rpp+' , ';
							str_record += list_itf.recordall+'<span>0</span>'+list_itf.recordnum+' , '+'<span>0</span>'+'/'+'<span>0</span>'+list_itf.papename;
							$('#pageRecord').html(str_record);
							
							//显示页码导航
							var str_page = '';
							str_page+='<li class="disabled"><a title="1"><span>'+list_itf.first+'</span></a></li>';
							str_page+='<li class="prev disabled"><a title="0">← <span>'+list_itf.prev+'</span></a></li>';
							str_page+='<li class="active"><a title="1">1</a></li>';
							str_page+='<li class="next disabled"><a title="2"><span>'+list_itf.next+'</span> →</a></li>';
							str_page+='<li class="disabled"><a title="1"><span>'+list_itf.last+'</span></a></li>';
							$('#pagePaginate').html(str_page);
							
							return false;
						}
						var colModelObj = listParam.colModel;
						var str_th = '<thead class="flip-content">';
						str_th += '<tr>';
						for(var i=0; i<colModelObj.length ; i++){
							if(colModelObj[i].display == 'checkbox'){
								str_th += '<th style="width:8px;"><input type="checkbox" class="group-checkable" id="checkAll"/></th>';
							}else{
								var css = '';
								if(colModelObj[i].ordertable){
									css = 'sorting';
								}
								if(listParam.orderName == colModelObj[i].name){
									if(listParam.orderDirection=='asc'){
										css = 'sorting_asc';
									}else{
										css = 'sorting_desc';
									}
								}
								css += ' '+colModelObj[i].myclass;
								
								str_th += '<th class="'+css+'" fieldname="'+colModelObj[i].name+'">'+colModelObj[i].display+'</th>';							
							}
							
						}
						str_th += '</tr>';
						str_th += '</thead>';
						
						var str_td = '<tbody>';
						for(var j=0; j<data.rows.length ; j++){
							str_td += '<tr class="odd gradeX">';
							for(var k=0; k<colModelObj.length ; k++){
								if(colModelObj[k].display == 'checkbox'){
									str_td += '<th style="width:8px;"><input type="checkbox" class="checkboxes" name="'+colModelObj[k].name+'" value="'+data.rows[j][colModelObj[k].name]+'"/></th>';
								}else{								
									str_td += '<td class="'+colModelObj[k].myclass+'">'+data.rows[j]['cell'][colModelObj[k].name]+'</td>';						
								}
							}
							str_td += '</tr>';
						}
						str_td += '</tbody>';
						
						$('#'+listParam.listId).html(str_th+str_td);
						
						
						//页码记录显示
						var str_record = '';
						str_record += '<select name="pageNum" class="m-wrap small">';
						str_record += '<option value="5">5</option>';
						str_record += '<option value="10">10</option>';
						str_record += '<option value="20">20</option>';
						str_record += '<option value="50">50</option>';
						str_record += '<option value="100">100</option>';
						str_record += '<option value="-1">All</option>';
						str_record += '</select> '+list_itf.rpp+' , ';
						str_record += list_itf.recordall+'<span>'+data.total+'</span>'+list_itf.recordnum+' , '+'<span>'+data.page+'</span>'+'/'+'<span>'+data.totalPage+'</span>'+list_itf.papename;
						$('#pageRecord').html(str_record);
						
						
						//页码导航
						var first = 1; //第一页
						var prev = listParam.nowPage-1; //下一页
						var next = listParam.nowPage+1; //上一页
						var last= data.totalPage; //最后页
						var totalPage = parseInt(data.totalPage);
						var nowPage = parseInt(data.page);
						var str_page = '';
						if(nowPage>1){
							str_page += '<li class=""><a title="1"><span>'+list_itf.first+'</span></a></li>';
							str_page += '<li class="prev"><a title="'+prev+'">← <span>'+list_itf.prev+'</span></a></li>';
						}else{
							str_page += '<li class="disabled"><a title="1"><span>'+list_itf.first+'</span></a></li>';
							str_page += '<li class="prev disabled"><a title="'+prev+'">← <span>'+list_itf.prev+'</span></a></li>';
						}
						
						var iMin = 0;
						var iMax = 0;
						if(totalPage==0){
							iMin = 1;
							iMax = 1;
						}else if(totalPage<=5){
							iMin = 1;
							iMax = totalPage;
						}else{
							if((nowPage-2)<1){
								iMin = 1;
								iMax = 5;
							}else if((nowPage-2)>=1 && (nowPage+2)<=totalPage){
								iMin = nowPage-2;
								iMax = nowPage+2;
							}else if((nowPage+2)>totalPage){
								iMin = totalPage-4;
								iMax = totalPage;
							}
						}
						
						for(var h=iMin; h<=iMax; h++){
							if(h==nowPage){
								str_page += '<li class="active"><a title="'+h+'">'+h+'</a></li>';
							}else{
								str_page += '<li><a title="'+h+'">'+h+'</a></li>';
							}
						}
						
						if(nowPage<totalPage){
							str_page += '<li class=""><a title="'+next+'"><span>'+list_itf.next+'</span> →</a></li>';
							str_page += '<li class="prev"><a title="'+last+'"><span>'+list_itf.last+'</span></a></li>';
						}else{
							str_page += '<li class="next disabled"><a title="'+next+'"><span>'+list_itf.next+'</span> →</a></li>';
							str_page += '<li class="disabled"><a title="'+last+'"><span>'+list_itf.last+'</span></a></li>';
						}
						
						$('#pagePaginate').html(str_page);
						
						$('select[name="pageNum"]').val(listParam.pageNum);
					},
					'json'
				);
			}
			
			getList();
			
			//每页页码改变时
			$('select[name="pageNum"]').live('change',function(){
				listParam.pageNum = parseInt($(this).val());
				listParam.nowPage = 1;
				
				getList();
			});
			
			//点击页码导航时
			$('#pagePaginate a').live('click',function(){				
				var nowPage = parseInt($(this).attr('title'));
				var maxPage = parseInt($('#pageRecord>span').eq(2).html());
				if(nowPage < 1 || nowPage > maxPage){
					return false;
				}
				listParam.nowPage = nowPage;
				getList();
			});
			
			//搜索
			$('#btn_search').on('click',function(){
				listParam.getSearch(listParam.where);
				
				getList();
			});			
			$(document).on('keydown',function(e){
				var key = e.which;
 
				if (key == 13) {
					listParam.getSearch(listParam.where);
				
					getList();		
				}
			});
			
			//排序
			$('#'+listParam.listId+' thead tr th').live('click',function(){
				if($(this).is(".sorting") || $(this).is(".sorting_asc") || $(this).is(".sorting_desc")){
					listParam.orderName = $(this).attr('fieldname');
					
					//如果是可以排序的
					if($(this).is(".sorting")){
						listParam.orderDirection = 'asc';
						$(this).removeClass(".sorting");
						$(this).addClass(".sorting_asc");
					}
					//如果是顺序排序
					if($(this).is(".sorting_asc")){
						listParam.orderDirection = 'desc';
						$(this).removeClass(".sorting_asc");
						$(this).addClass(".sorting_desc");
					}
					//如果是顺序排序
					if($(this).is(".sorting_desc")){
						listParam.orderDirection = 'asc';
						$(this).removeClass(".sorting_desc");
						$(this).addClass(".sorting_asc");
					}
					
					getList();
				}
			});
			
			//批量选择
			$('#checkAll').live('click',function(){				
				if($(this).is(":not(:checked)")){
					$('#'+listParam.listId+' tbody .checkboxes').removeAttr('checked');
				}else{
					$('#'+listParam.listId+' tbody .checkboxes').attr('checked','checked');
				}
			});
			
			//批量删除
			$('#batchDelete').live('click',function(){
				var param = '';
				$('.checkboxes').each(function(index, element) {
					if($(this).is(":checked")){
						if(param==''){
							param += $(element).val();
						}else{
							param += ','+$(element).val();
						}
					}
                });
				
				$.get(ABS_URL_Module+'/del/id/'+param);
				
				getList();
			});
			
			//批量获取Id
			/*$('#bulkMail').live('click',function(){
				var param = '';
				$('.checkboxes').each(function(index, element) {
					if($(this).is(":checked")){
						if(param==''){
							param += $(element).val();
						}else{
							param += ','+$(element).val();
						}
					}
                });
				
				window.location.href=ABS_URL_Module+'/sendmail/id/'+param;
			});*/
		}
    };
}();